// pages/studyLogger/studyLogger.js
import Dialog from '../../plugin/vant/dialog/dialog';
import StudyLoggerApi from '../../api/studyLogger.js'
import CaseStudyApi from "../../api/caseStudy";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewHeight:0,
    studyLoggerList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryStudyRecordList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面首次渲染完毕时执行
    let height = wx.getSystemInfoSync().windowHeight
    let $navBar = this.selectComponent("#navBar")
    let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
    let viewHeight = height - navBarHeight
    this.setData({
      viewHeight:viewHeight
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handlerGobackClick(){
    // wx.redirectTo({
    //   url: '/pages/caseStudy/caseStudy'
    // })
    wx.navigateBack()
  },
  /**
   * 清空历史数据
   */
  emptyLogger(){
    Dialog.confirm({
      title: '是否清除学习记录?',
    }).then(async () => {
      // on confirm
      const studentID = wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).id:''
      const param = {
        studentId :studentID
      }
      await StudyLoggerApi.deleteStudyRecordList(param)
      this.queryStudyRecordList()
    }).catch(() => {
        // on cancel
      });
  },
  /**
   * 查询历史记录
   */
  async queryStudyRecordList(){
    const studentID = wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).id:''
    const param = {
      studentId :studentID
    }
    const res = await StudyLoggerApi.getStudyRecordList(param)
    const list = []
    res.data.forEach(item=>{
      if(item!=null ){
        item.createTime = new Date(item.createTime).getTime()
        list.push(item)
      }
    })
    this.setData({
      studyLoggerList:list
    })
  },
  /**
   * 查看成绩详情
   */
  goToPracticeResults(e){
    const obj = e.currentTarget.dataset['obj']
    const examId = obj.examselfId
    wx.navigateTo({
      url: `/pages/practiceResults/practiceResults?examId=${examId}`
    })
  },
  startStudy(e) {
    const obj = e.currentTarget.dataset['obj']
    const id = obj.caseId
    wx.navigateTo({
      url: `/pages/caseStudyTest/caseStudyTest?id=${id}`
    })
  }
})
